package com.rovinj_server.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.rovinj_server.R;
import com.rovinj_server.entity.InventorySub;
import com.rovinj_server.entity.InventorySub;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class FragmentRecordAdapter extends BaseAdapter{

	private Context mContext;

	private List<InventorySub> mListDataBean = new ArrayList<>();
	private HashMap<Integer,Boolean> isSelected;
	public int showCount = 20;
	public FragmentRecordAdapter(Context context, List<InventorySub> mListDataBean){
		this.mContext = context;
		this.mListDataBean = mListDataBean;
		isSelected = new HashMap<Integer, Boolean>();
		initDate();
	}

	// 初始化isSelected的数据
	private void initDate(){

		for(int i=0; i<mListDataBean.size();i++) {
			getIsSelected().put(i,false);
		}
	}

	public HashMap<Integer,Boolean> getIsSelected() {
		return isSelected;
	}

	public  void setIsSelected(HashMap<Integer,Boolean> isSelected) {
		this.isSelected = isSelected;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
//		return mList.size();

		return  showCount > mListDataBean.size() ? mListDataBean.size() : showCount;
	}

	@Override
	public Object getItem(int i) {
		// TODO Auto-generated method stub
		return mListDataBean.get(i);
	}

	@Override
	public long getItemId(int i) {
		// TODO Auto-generated method stub
		return mListDataBean.size();
	}

	@Override
	public View getView(int i, View convertView, ViewGroup viewgroup) {
		 List<InventorySub> list = new ArrayList<>();
		if (mListDataBean.size() > showCount){
			list = mListDataBean.subList(0,showCount);
		}else {
			list = mListDataBean;
		}

		ViewHolder holder;
		if (convertView !=null) {
			holder = (ViewHolder) convertView.getTag();
		} else {

			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_record_fragment, viewgroup, false);
			holder.Location = (TextView) convertView.findViewById(R.id.Location);
			holder.qty1 = (TextView) convertView.findViewById(R.id.qty1_num);
			holder.qty2 = (TextView) convertView.findViewById(R.id.qty2_num);
			holder.qty2 = (TextView) convertView.findViewById(R.id.qty2_num);
			holder.barode = (TextView) convertView.findViewById(R.id.barode);

			holder.	code = (TextView) convertView.findViewById(R.id.code);
			holder.	Sjnumber = (TextView) convertView.findViewById(R.id.Sjnumber);
			holder.	diffNumber = (TextView) convertView.findViewById(R.id.diffNumber);
			holder.	img = (ImageView) convertView.findViewById(R.id.img);
			//holder.cb = (CheckBox) convertView.findViewById(R.id.fahuo_kbox);
			convertView.setTag(holder);
			AutoUtils.autoSize(convertView);
		}

		InventorySub inventoryBaen = mListDataBean.get(i);
		holder.Location.setText(inventoryBaen.getLocation());
        holder.qty1.setText(inventoryBaen.getMoreThan()+"");
        holder.qty2.setText(inventoryBaen.getLessThan()+"");
        holder.barode.setText(inventoryBaen.getBarode()+"");
		holder.code.setText(inventoryBaen.getCode());
		holder.Sjnumber.setText(inventoryBaen.getSjnumber());
		holder.diffNumber.setText(inventoryBaen.getDiscNumber()+"");
		String state = inventoryBaen.getState();

		 if(state!=null&&state.equals("完成")){
			 holder.img.setImageResource(R.drawable.gous);
		 }else {
			 holder.img.setVisibility(View.GONE);
		 }
		//holder.cb.setChecked(getIsSelected().get(i));
		return convertView;
	}

	public void setListDataBean(List<InventorySub> listDataBean) {
		mListDataBean = listDataBean;
	}

	public static class ViewHolder {
		public TextView  Location;
		public TextView GoodsName;
		public	TextView sizeName;
		public 	TextView code;
		public	TextView type;
		public	TextView Sjnumber;
		public	TextView diffNumber;
		public	TextView qty1;
		public	TextView qty2;
		public	TextView barode;

		public ImageView img;
		public CheckBox cb;
	}

}
